package com.wanmait.employment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.employment.aop.LogInfo;
import com.wanmait.employment.entity.Admin;
import com.wanmait.employment.entity.Industry;
import com.wanmait.employment.entity.Position;
import com.wanmait.employment.entity.PositionType;
import com.wanmait.employment.mapper.AdminMapper;
import com.wanmait.employment.mapper.IndustryMapper;
import com.wanmait.employment.mapper.PositionMapper;
import com.wanmait.employment.mapper.PositionTypeMapper;
import com.wanmait.employment.service.PositionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
@Transactional(rollbackFor = Exception.class)
public class PositionServiceImpl extends ServiceImpl<PositionMapper,Position> implements PositionService {

    @Resource
    private PositionMapper positionMapper;
    @Resource
    private PositionTypeMapper positionTypeMapper;
    @Resource
    private IndustryMapper industryMapper;
    @Resource
    private AdminMapper adminMapper;


    @Override
    public PageInfo<Position> list( String title, Integer pageNum, Integer pageSize) {

//        industry - positionType - position
        PageHelper.startPage(pageNum,pageSize);
        LambdaQueryWrapper<Position> positionLambdaQueryWrapper=new LambdaQueryWrapper<>();
        positionLambdaQueryWrapper.like(Position::getTitle,title);
        List<Position> positionList=positionMapper.selectList(positionLambdaQueryWrapper);
        PageInfo<Position> pageInfo=new PageInfo<>(positionList);
        pageInfo.getList().forEach(position -> {
            LambdaQueryWrapper<PositionType> positionTypeLambdaQueryWrapper=new LambdaQueryWrapper<>();
            positionTypeLambdaQueryWrapper.eq(PositionType::getId,position.getPositionTypeId());
            PositionType positionType=positionTypeMapper.selectOne(positionTypeLambdaQueryWrapper);
            position.setPositionType(positionType);

            LambdaQueryWrapper<Industry> industryLambdaQueryWrapper=new LambdaQueryWrapper<>();
            industryLambdaQueryWrapper.eq(Industry::getId,positionType.getIndustryId());
            Industry industry=industryMapper.selectOne(industryLambdaQueryWrapper);
            position.setIndustry(industry);

            LambdaQueryWrapper<Admin> adminLambdaQueryWrapper=new LambdaQueryWrapper<>();
            adminLambdaQueryWrapper.eq(Admin::getId,position.getAdminId());
            Admin admin=adminMapper.selectOne(adminLambdaQueryWrapper);
            position.setAdmin(admin);
        });
        return pageInfo;
    }

    @Override
    public List<Position> listAll() {

        return positionMapper.selectListAll();
    }

    @LogInfo("添加/更新职位类型")
    @Override
    public void update(Position position) {
        System.out.println(position+"/////////////////////////////");
        if (position.getId()!=null){
            positionMapper.updateById(position);
        }else{
            position.setIsPopular(false);
            positionMapper.insert(position);

        }

    }


}
